﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 图书借阅系统
{
    public partial class 书名查询 : System.Web.UI.Page
    {
        private void BindGridView1()
        {
            string tta = "select * from 图书 where 书名 like '%" +TextBox1.Text + "%'";
            GridView1.DataSource = Connection.getData(tta);
            GridView1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindGridView1();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {



            Label2.Text = "";

            int lkbt = GridView1.SelectedIndex;
            string tt = GridView1.Rows[lkbt].Cells[1].Text.ToString();

            string sss = "Select 实际数量 from 图书 where id =" + tt;

            int num = Connection.getNum(sss);

            if (num > 0)
            {


                string str = "insert into 借阅 values (" + tt + "," + Session["userID"] + ",'" + System.DateTime.Now.ToLocalTime() + "','" + System.DateTime.Now.AddDays(+7) + "')";

                string ss = "Select * from 借阅 where 图书id =" + tt + "and 用户id = " + Session["userID"];
                if (Connection.getData(ss) != null)
                {
                    Label2.Text = "已借阅！";
                }
                else
                {
                    if (Connection.setData(str) != -1)
                    {
                        Label2.Text = "借阅成功！";

                        string ssss = "update 图书 set 实际数量 = 实际数量-1 where id =" + tt;
                        Connection.setData(ssss);
                        BindGridView1();
                    }
                    else
                    {
                        Label2.Text = "借阅失败！";
                    }
                }
            }
            else
            { Label2.Text = "图书已经没有了！"; }

        }

    }
}